<?php
/**
 * Created by PhpStorm.
 * User: dx
 * Date: 17/2/23
 * Time: 下午午13:23
 */
class AhpUsers extends Eloquent
{
    protected $table = "ahp_user";

    public static function getUser($user_name,$password){
        return AhpUsers::where('user_name', $user_name)->where('password', $password)->first();
    }
    public static function getUserList($data){
        $page = empty($data['page'])?1:$data['page'];
        $page_size = empty($data['page_size'])?10:$data['page_size'];
        $ahp_users = AhpUsers::whereIn('status',array(1,2));
        if(!empty($data['phone'])){
            $ahp_users->where('phone',$data['phone']);
        }
        if(!empty($data['real_name'])){
            $ahp_users->where('real_name','like','%'.$data['real_name'].'%');
        }
        if(!empty($data['start_time']) && !empty($data['end_time'])){
            $ahp_users->whereBetween('created_at',[$data['start_time'].' 00:00:00',$data['end_time'].' 23:59:59']);
        }
        $count = $ahp_users->count();
        $users = $ahp_users->orderBy('created_at','desc')->skip($page_size*($page-1))->take($page_size)->get();
        $user_list = array(
            'count' => $count,
            'data' => $users,
        );
        return $user_list;
    }
    public static  function getUserDetails($user_id){
        return AhpUsers::where('id',$user_id)->first();
    }

    public function ahp_role()
    {
        return $this->hasOne('AhpRole',"id","role_id");
    }
}